<template>
	<div class="role" v-if="dialogFormVisible">
		<el-dialog :title="title" :visible="dialogFormVisible" :close-on-click-modal="false" width="800px" :before-close="handleClose">
			<div class="tab">
				<el-form ref="form" :rules="rules" :model="form" label-width="80px">
					<el-form-item label="产品线" prop="product_line_id">
						<el-select
							v-model="form.product_line_id"
							placeholder="请选择产品线"
							style="width:100%;"
							@change="parentChange">
							<el-option
								v-for="item in options"
								:key="item.index"
								:label="item.name"
								:value="item.id">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="产品名称" prop="name">
						<el-input v-model="form.name" placeholder="请输入产品名称"></el-input>
					</el-form-item>
					<el-form-item label="产品状态">
						<el-select v-model="form.status" placeholder="请选择状态" style="width:100%;" >
							<el-option label="禁用" :value="0"></el-option>
							<el-option label="正常" :value="1"></el-option>
						</el-select>
					</el-form-item>
				</el-form>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button @click="handleClose">取 消</el-button>
				<el-button type="primary" @click="add('form')">提 交</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { getHeader } from '@/app.config'
export default {
	name: 'addForm',
	data(){
		return {
			dialogFormVisible:false,
			form:{
				name:'',
				status:1
			},
			id:'',
			rules:{
				// name: 36
				product_line_id:[{required: true, message: '请选择产品线', trigger: 'change'}],
				name: [
					{max: 36, message: '产品名称不能超过36个字符', trigger: 'blur'},
					{ required: true, message: '请输入产品名', trigger: 'blur' }
				],
			},
			options:{},
			num:0,
			title:'新增'
		}
	},
	created() {

	},
	methods:{
		parentChange(e){
			console.log(e,1212121)
		},
		handleClose(){
			this.dialogFormVisible = false
			this.form = {}
		},
		add(form){
			this.$refs[form].validate((valid) => {
				if (valid) {
					var source = `${process.env.VUE_APP_KANBAN_HOST}/api/v1/product`;
					var data = this.form
					this.$axios.post(source,data,{headers: getHeader()})
						.then((response) => {
							if (response.status == 200) {
								this.dialogFormVisible = false
								this.$parent.initTable()
								this.form = {}
								this.$message.success("新增成功!")
							}
						}).catch((err) => {
						console.log(err);
					});
				} else {
					return false;
				}
			});

		},
		initData(data){
			var source = `${process.env.VUE_APP_KANBAN_HOST}/api/v1/product/line/list`;
			this.$axios.get(source,{headers: getHeader()})
				.then((response) => {
					if (response.status == 200) {
						console.log(response.data.data,12121112)
						this.options = Object.assign({},response.data.data)
					}
				}).catch((err) => {
				console.log(err);
			});
		}
	}

}
</script>

<style lang="scss" scoped>
.role{
	.tab{

	}
}
</style>
